/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.user.action;

import caa.struts.user.form.ManageUserForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.DepartmentEntity;
import th.com.entity.FacultyEntity;
import th.com.entity.UserEntity;
import th.com.entity.UserTypeEntity;
import th.com.ku.CAAService;

/**
 *
 * @author Ketsaraporn
 */
public class UserManageAction extends DispatchAction {

    public ActionForward domanageUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        
        addUserForm.setUserEntity(new UserEntity());
        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addUserForm.setFacultyList(facList);
        ArrayList<UserTypeEntity> userTypeList = CAAService.selectUserType();
        addUserForm.setUserTypeList(userTypeList);
        
        List<UserEntity> rs = CAAService.selectUser(addUserForm.getKwKeyWord());
        addUserForm.setUserList(new ArrayList<UserEntity>(rs));
        return mapping.findForward("manageuser.success");
    }

    public ActionForward doListAddUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        ActionForward forward = new ActionForward();
        forward = mapping.findForward("listadduser.success");
        
        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addUserForm.setFacultyList(facList);
        System.out.println("doAddUser");
        if (addUserForm.getUserEntity() != null) {
            System.out.println("addUserForm.getUserEntity().getFacId() = " + addUserForm.getUserEntity().getFacId());
            List<DepartmentEntity> depList = CAAService.selectDepartmentAll(addUserForm.getUserEntity().getFacId());
            addUserForm.setDepartmentList(new ArrayList<DepartmentEntity>(depList));
        }

        if(!addUserForm.getMode().equalsIgnoreCase("")) {
            forward = mapping.findForward("edituser.success");
        }
  
        return forward;
    }

    public ActionForward doAddUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        //   new object ใหม่ เพื่อรับค่า                   
        
        //   เชื่อมต่อ database 
        String rs = CAAService.createUser(addUserForm.getUserEntity());
        
        addUserForm.setUserEntity(new UserEntity());


        return mapping.findForward("AddUser.success");
    }
    
    

    public ActionForward doedituser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        ActionForward forward = new ActionForward();
        forward = mapping.findForward("edituser.success");
        
        if(!addUserForm.getMode().equalsIgnoreCase("")) {
            List<UserEntity> list = CAAService.selectUserPreview(addUserForm.getUserId());
            addUserForm.setUserEntity(list.get(0));
            List<DepartmentEntity> depList = CAAService.selectDepartmentAll(addUserForm.getUserEntity().getFacId());
            addUserForm.setDepartmentList(new ArrayList<DepartmentEntity>(depList));
            
        } else {
            String rs = CAAService.updateUser(addUserForm.getUserEntity());
            forward = mapping.findForward("manageuser.do");
        }
        
//        return mapping.findForward("edituser.success");
        return forward;        
    }
    
    public ActionForward doSaveEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        addUserForm.setMode("");
        String rs = CAAService.updateUser(addUserForm.getUserEntity());
        
        return mapping.findForward("manageuser.do");        
    }

    public ActionForward dodeleteuser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        String rs = CAAService.deleteUser(addUserForm.getUserId());
        return mapping.findForward("manageuser.do");   
    }

    public ActionForward doPreviewUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageUserForm addUserForm = (ManageUserForm) form;
        List<UserEntity> rs = CAAService.selectUserPreview(addUserForm.getUserId());
        addUserForm.setUserEntity(rs.get(0));
        return mapping.findForward("previewuser.success");
    }
}
